home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr28 / ipxtcpip.zip / IPXTCPCF.C < prev    next >
Text File  |  1993-03-06  |  9KB  |  271 lines

  1. /*    IPXTCPCFG - address configuration for TCP via IPX
  2.  *
  3.  *    Jerzy Tarasiuk                12-Jul-91
  4.  *
  5.  *    Copyright (c) 1991, Jerzy Tarasiuk <JT@PLEARN>.
  6.  *    Free software. You can use, redistribute it and/or modify
  7.  *    it under the terms of the GNU General Public License
  8.  *    as published by the Free Software Foundation.
  9.  */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <dos.h>
  14. #include <nwbindry.h>
  15. #include <nwconn.h>
  16.  
  17. #define    cpleft    \
  18. "IPXTCPCFG - address configuration for TCP via IPX\n\
  19. Free software: NO WARRANTY. (c) 1991 <JT@PLEARN>.\n"
  20. #define    usage    \
  21. "\nIPXTCPCFG arguments:\n\
  22.  arg1    database file name (NetWare node list)\n\
  23.  arg2    gateway user name (as NetWare call gives)\n\
  24.  arg3    configuration file name ()\n\
  25.  arg4    name prefix (to precede station name)\n\
  26. (look documentation for file format specification)\n\n\
  27. Copyright (c) 1991, Jerzy Tarasiuk, <JT@PLEARN>\n\
  28. Free software - you can use and redistribute it,\n\
  29. see documentation in file READ.ME! for details.\n"
  30.  
  31. #define    mxconn    5    /* max # of NET connections to scan */
  32. #define    mxinf    80
  33. #define    mxnam    15
  34. #define    mxbuf    256
  35. #define    mxcwd    64
  36. #define    local    static
  37. #define    global
  38.  
  39. typedef    unsigned char    bool;
  40. typedef    unsigned char    byte;
  41. typedef    unsigned int    word;
  42.  
  43. local    bool    AdrIdn(ea1,ea2)
  44.     byte    ea1[6],ea2[6];
  45. {    int    i;
  46.     for (i=0;i<6;i++)    if (ea1[i]!=ea2[i])    return    0;
  47.         return    1;
  48. }
  49.  
  50. local    bool    ChkLog(uinfo,uname)
  51.     char    *uinfo,*uname;
  52. {    word    conmax,conind,connum,uindx;
  53.     bool    ckadr;
  54.     byte    netadr[6],netnum[4],phyadr[6];
  55.     WORD    cnnlst[mxconn],concnt,socket;
  56.         ckadr=uinfo!=NULL;
  57.     if (ckadr)    for (uindx=0;uindx<6;uindx++)
  58.                 sscanf(uinfo+2*uindx,"%2x",netadr+uindx);
  59.     GetObjectConnectionNumbers(uname,OT_USER,&concnt,cnnlst,mxconn);
  60.         if (!ckadr)    return    concnt>0;
  61.     conmax=min(concnt,mxconn);
  62.     for (conind=0;conind<conmax;conind++)
  63.     {    connum=cnnlst[conind];
  64.         GetInternetAddress(connum,netnum,phyadr,&socket);
  65.         if (AdrIdn(phyadr,netadr))    return    1;
  66.     }    if (concnt<=mxconn)    return    0;
  67.     for (connum=cnnlst[mxconn-1]+1;connum<250;connum++)
  68.     {    socket=0xFFFF;
  69.         GetInternetAddress(connum,netnum,phyadr,&socket);
  70.         if (socket!=0xFFFF)
  71.                     if (AdrIdn(phyadr,netadr))    return    1;
  72.     }    return    0;
  73. }
  74.  
  75. local    void    Error(msg)    /* error message and exit */
  76.     char    *msg;
  77. {    printf("*** %s ***\n",msg);
  78.     exit(4);
  79. }
  80.  
  81. local    char    *Insert(cur,end,str)
  82.     char    *cur,*end,*str;
  83. {    char    tmp[mxbuf+1];
  84.     int    lng;    lng=strlen(str);
  85.     if (cur+lng>end)    Error("String buffer overflow.");
  86.     strcpy(tmp,cur);    strcpy(cur,str);    cur+=lng;
  87.     strcpy(cur,tmp);    return    cur;
  88. }
  89.  
  90. local    void    NoFile(fname)
  91.     char    *fname;
  92. {    printf("Cannot open file: %s\n",fname);
  93.     printf("Invoke without arguments for help.");
  94.         exit(4);
  95. }
  96.  
  97. local    void    UsageInfo(argc)    /* display usage help and exit */
  98.     int    argc;
  99. {    if (argc>1)    Error("Insufficient arguments (no arg = help).");
  100.     printf(usage);    exit(0);
  101. }
  102.  
  103. local    void    GetFarS(target,segment,offset,length)
  104.     char    *target;    /* get string from far address    */
  105.         word    segment,offset,length;    /* any memory model    */
  106. {    word    index=0;
  107.     while (index<length)
  108.         *target++=peekb(segment,offset+index++);
  109. }
  110.  
  111. local    bool    IpxPkt(vect)    /* verify packet driver name="IPX"    */
  112. {    union    REGS    r;    /* don't work with some old drivers    */
  113.     struct    SREGS    s;    /* (specs 1.07 and before, probably    */
  114.         char    n[5];    n[4]=0;    /*  no such IPXPKT driver existed)    */
  115.     segread(&s);    r.x.ax=0x01FF;    r.x.bx=0xFFFF;
  116.         int86x(vect,&r,&r,&s);
  117.         if (r.x.flags&1)    return    0;        /* if old driver - bad */
  118.         GetFarS(n,s.ds,r.x.si,4);
  119. /*      printf("Packet driver: \"%s\"\n",n);
  120.     printf("version %5d, type %5d, class %3d, number %3d, funct %1d\n",
  121.     n,r.x.bx,r.x.dx,r.h.ch,r.h.cl,r.h.al,-1);    */
  122.         return    !strcmp(n,"IPX");    /* 1 (=OK) if IPXPKT driver */
  123. }
  124.  
  125. local    bool    GetVect(vect)    /* look for IPXPKT or free vector */
  126.     int    *vect;
  127. {    word    vs2,vec,vps,vpo,vfr=0;
  128.     char    tmp[13];    tmp[12]='\0';
  129.     for (vec=0x60;vec<=0x80;vec++)    /* scan 0x60..0x80 (inclusive) */
  130.         {    vs2=vec<<2;
  131.             vps=peek(0,vs2+2);
  132.             vpo=peek(0,vs2);
  133.         if (!(vps|vpo))    /* unused vector - save number as free */
  134.                 {    if (!vfr)    vfr=vec;    continue;    }
  135.                 GetFarS(tmp,vps,vpo,12);    /* get possible signature */
  136.                 if (strcmp(tmp+3,"PKT DRVR"))    continue;    /* .. bad */
  137.         if (IpxPkt(vec))    /* IPXPKT driver at this vector */
  138.                 {    *vect=vec;    return    1;    }
  139.         }    *vect=vfr;    return    0;    /* no driver found */
  140. }
  141.  
  142. local    char    name[mxnam+1];    /* local computer name */
  143. local    word    myconn;        /* server connection name */
  144.  
  145. local    int    GetNumb(fname,gwusr)
  146.     char    *fname,*gwusr;    /* assign unique number for computer */
  147. {    byte    phyadr[6];    /* and check if gateway is available */
  148.     char    uinfo[mxinf+1],*uiptr;
  149.     FILE    *users;
  150.         int    numb=0,temp,mxun=1,lookgw=1;
  151.     myconn=GetConnectionNumber();
  152.     if (!myconn)    Error("Non-network komputer or net fault.");
  153.     if ((users=fopen(fname,"rt"))==NULL)    return    0;
  154.     GetStationAddress(phyadr);
  155.     sprintf(name,"%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x ",
  156.         phyadr[0],phyadr[1],phyadr[2],phyadr[3],phyadr[4],phyadr[5]);
  157.     while (fgets(uinfo,mxinf,users)!=NULL)    /* scan database */
  158.     { uiptr=uinfo; while (*uiptr==' ') uiptr++;
  159.           while (*uiptr!='\0')    if (*++uiptr==' ')    break;
  160.           while (*uiptr==' ') *uiptr++='\0';
  161.           if (sscanf(uinfo,"%d",&temp))
  162.         if (temp)
  163.           if (temp==1)
  164.         { if (lookgw)    if (ChkLog(uiptr,gwusr))    lookgw=0;
  165.                 else                lookgw=-1;
  166.                 }
  167.               else    if (!strncmpi(name,uiptr,13))
  168.         { numb=temp; strncpy(name,uiptr+13,mxnam); name[mxnam]='\0';
  169.           for (temp=0;name[temp]!='\0';temp++)
  170.                     if (name[temp]==' ') name[temp]='_';
  171.               while (temp--)    if (name[temp]!='_')    break;
  172.                     else    name[temp]='\0';
  173.         }    else    mxun=max(mxun,temp);
  174.     }
  175.         if (lookgw>0)    if (ChkLog(NULL,gwusr))    lookgw=0;
  176.         if (lookgw)    Error("Gateway not logged on.");
  177.         if (ferror(users))    /* check for file read error */
  178.     {    perror("IPXTCPCF user list");
  179.         exit(4);
  180.     }    fclose(users);
  181.     if (!numb)    numb=mxun+myconn; /* if not found in database */
  182.            return    numb;
  183. }
  184.  
  185. local    bool    pktf;
  186. local    int    numb,vect;
  187.  
  188. local    void    Configure(cfgname,prfx)
  189.     char    *cfgname,*prfx;
  190. /*
  191.  *    The "configuration" means creating several files which names
  192.  *    are specified in the configuation file, in current directory.
  193.  *    The data for them is also in the configuration file.
  194.  *    After it is read, each line is scanned for special sequences
  195.  *    which, if found, are processed before writing the line.
  196.  *    $$ - put $ here
  197.  *    $c - ignore remainder of this line (comment)
  198.  *    $d - put current directory here
  199.  *    $f - open new output file
  200.  *    $l - write line if !pktf
  201.  *    $n - put name here
  202.  *    $i - put numb here
  203.  *    $v - put vect here
  204.  */
  205. {    FILE    *cfg,*out;    bool    wrt;
  206.     char    act,buf[mxbuf+1],cod[9],cwd[mxcwd+1],*cur,*end,*tmp;
  207.     end=buf+mxbuf;    if (getcwd(cwd,mxcwd)==NULL)    Error("Cannot GetDir.");
  208.     if ((out=fopen("CON","wt"))==NULL)    Error("Cannot write to CON");
  209.     if ((cfg=fopen(cfgname,"rt"))==NULL)           NoFile(cfgname);
  210.     while ((fgets(buf,mxbuf,cfg))!=NULL)
  211.     {    cur=buf;
  212.         for (wrt=1;(cur=strchr(cur,'$'))!=NULL;)
  213.         {    strcpy(cur,cur+1);
  214.                            if ((act=*cur)=='$')
  215.             {    cur++;    continue;    }
  216.             strcpy(cur,cur+1);
  217.                            switch (act)
  218.             { case 'c':    wrt=cur!=buf;
  219.                     *cur++='\n';    *cur='\0';
  220.                     break;
  221.                           case 'd':    cur=Insert(cur,end,cwd);
  222.                             break;
  223.               case 'f':    goto    reopen;
  224.               case 'l':    wrt=!pktf;
  225.                     break;
  226.               case 'n':    cur=Insert(cur,end,prfx);
  227.                     cur=Insert(cur,end,name);
  228.                     break;
  229.               case 'i':    sprintf(cod,"%d",numb);
  230.                     cur=Insert(cur,end,cod);
  231.                     break;
  232.                           case 'v':    sprintf(cod,"0x%2.2X",vect);
  233.                     cur=Insert(cur,end,cod);
  234.                     break;
  235.                           default:    Error("Bad config file.");
  236.                         }
  237.                 }
  238.         if (wrt) fputs(buf,out);
  239.                 continue;
  240.     reopen:    fclose(out);
  241. /*        cur+=strspn(cur," ");
  242.                 if (*(tmp=cur+strlen(cur)-1)=='\n')    *tmp='\0';
  243.  */        *cur='\0';
  244.                 printf("Creating \"%s\".\n",buf);
  245.         if ((out=fopen(buf,"wt"))==NULL)
  246.                 Error("File creation error.");
  247.         }    fclose(out);
  248. }
  249.  
  250. global    void    main(argc,argv)
  251.     int    argc;
  252.     char    *argv[];
  253. /*
  254.  *    argv[0]    program name (not used)
  255.  *    argv[1]    database file name
  256.  *    argv[2] gateway user name
  257.  *    argv[3]    configuration file name
  258.  *    argv[4]    name prefix (optional)
  259.  */
  260. {    printf(cpleft);    pktf=GetVect(&vect);
  261.     if (!vect)    Error("No vector for packet driver.");
  262. /*    if (!pktf)    printf("Load ");
  263.     printf("IPX packet driver at vector 0x%2.2x\n",vect);    */
  264.     if (argc<4)    UsageInfo(argc);
  265.     numb=GetNumb(argv[1],argv[2]);
  266.     if (!numb)    NoFile(argv[1]);
  267. /*    printf("numb=%3.3d, name=\"%s\"\n",numb,name);    */
  268.         if (argc>4)    Configure(argv[3],argv[4]);
  269.         else        Configure(argv[3],"");
  270. }
  271.